/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dto.LocationDTO;
import east2west.dto.SchedulesDTO;
import east2west.dto.TourDTO;
import east2west.dto.TourDetailDTO;
import east2west.dto.TourPriceDTO;
import east2west.service.TourService;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author anhtuan
 */
public class insertTourAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERR = "err";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        boolean flag = true;
        javax.servlet.ServletConfig config = servlet.getServletConfig();
        TourService serTour = new TourService();
        TourDTO tourDTO = new TourDTO();
        ArrayList<TourPriceDTO> listTourprice = new ArrayList<TourPriceDTO>();
        TourDetailDTO tourDetail = new TourDetailDTO();
        ArrayList<TourDetailDTO> listTourDetail = new ArrayList<TourDetailDTO>();
        SchedulesDTO scheduleDTO = new SchedulesDTO();
        ArrayList<SchedulesDTO> listSchedule = new ArrayList<SchedulesDTO>();
        //upload anh
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
        } else {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                if (item.isFormField()) {
                    String name = item.getFieldName();
                    if (name.equals("tourName")) {
                        tourDTO.setTourName(item.getString());
                    }
                    if (name.equals("intro")) {
                        tourDTO.setIntroduction(item.getString());
                    }
                    if (name.equals("tourType")) {
                        int tourType = Integer.parseInt(item.getString());
                        tourDTO.setTypeId(tourType);
                    }
                    if (name.equals("Departure")) {

                        tourDTO.setDeparture(item.getString());
                    }
                    if (name.equals("Destination")) {

                        tourDTO.setDestination(item.getString());
                    }
                    if (name.equals("Duration")) {
                        int Duration = Integer.parseInt(item.getString());
                        tourDTO.setDuration(Duration);
                    }
                    //end tourDTO
                    //serviceDTO
                    if (name.equals("service")) {
                        int service = Integer.parseInt(item.getString());
                        tourDetail.setTourServiceId(service);
                    }
                    if (name.equals("detail")) {
                        tourDetail.setDetail(item.getString());
                    }
                    if (name.equals("priceser")) {
                        float priceser = Float.parseFloat(item.getString());
                        tourDetail.setPrice(priceser);
                        listTourDetail.add(tourDetail);
                        tourDetail = new TourDetailDTO();
                    }
                    //tourprice
                    if (name.equals("onep")) {
                        int onep = Integer.parseInt(item.getString());
                        TourPriceDTO priceDTO = new TourPriceDTO();
                        priceDTO.setQuantity(1);
                        priceDTO.setPrice(onep);
                        listTourprice.add(priceDTO);
                    }
                    if (name.equals("twop")) {
                        int twop = Integer.parseInt(item.getString());
                        TourPriceDTO priceDTO = new TourPriceDTO();
                        priceDTO.setQuantity(2);
                        priceDTO.setPrice(twop);
                        listTourprice.add(priceDTO);
                    }
                    if (name.equals("forp")) {
                        int forp = Integer.parseInt(item.getString());
                        TourPriceDTO priceDTO = new TourPriceDTO();
                        priceDTO.setQuantity(3);
                        priceDTO.setPrice(forp);
                        listTourprice.add(priceDTO);
                    }
                    if (name.equals("overp")) {
                        int overp = Integer.parseInt(item.getString());
                        TourPriceDTO priceDTO = new TourPriceDTO();
                        priceDTO.setQuantity(4);
                        priceDTO.setPrice(overp);
                        listTourprice.add(priceDTO);

                    }
                    //localtion
                    if (name.equals("localtion")) {

                        int localtion = Integer.parseInt(item.getString());
                        scheduleDTO.setLocationId(localtion);
                    }
                    if (name.equals("desSche")) {
                        scheduleDTO.setDescription(item.getString());
                        listSchedule.add(scheduleDTO);
                        scheduleDTO = new SchedulesDTO();
                    }
                } else {
                    try {
                        String itemName = item.getName();
                        tourDetail.setImagesUrl("images/Tour/" + itemName);
                        String url = config.getServletContext().getRealPath("/");
                        String url1 = url.substring(0, (url.length() - 10));
                        File savedFile = new File(url1 + "/web/images/Tour/" + itemName);
                        item.write(savedFile);
                        //request.setAttribute("url", url1);

                    } catch (Exception e) {
                        flag = false;
                    }
                }
            }
        }
        boolean flaginst = true;
        if (flag) {
            int tourId = serTour.createTour(tourDTO);
            if (tourId > 0) {
                tourDTO.setTourId(tourId);
                tourDetail.setTourId(tourId);
                for (Iterator<TourPriceDTO> it = listTourprice.iterator(); it.hasNext();) {
                    TourPriceDTO tourPriceDTO = it.next();
                    tourPriceDTO.setTourId(tourId);
                    if (!serTour.createTourPrice(tourPriceDTO)) {
                        flaginst = false;
                    }
                }
                //SchedulesDTO
                for (Iterator<SchedulesDTO> it = listSchedule.iterator(); it.hasNext();) {
                    SchedulesDTO schedulesDTO = it.next();
                    schedulesDTO.setTourId(tourId);
                    if (serTour.creatSchedule(schedulesDTO)) {
                    } else {
                        flaginst = false;
                    }
                }
                for (Iterator<TourDetailDTO> it = listTourDetail.iterator(); it.hasNext();) {
                    TourDetailDTO tourDetailsDTO = it.next();
                    tourDetailsDTO.setTourId(tourId);
                    if (serTour.createTourDetail(tourDetailsDTO)) {
                    } else {
                        flaginst = false;
                    }
                }

            } else {
                flaginst = false;
            }
        } else {
            flaginst = false;
        }
        if (flaginst) {
            return mapping.findForward(SUCCESS);
        } else {
            return mapping.findForward(ERR);
        }

//        request.setAttribute("TourDTO", tourDTO);
//        request.setAttribute("listservice", listTourDetail);
//        request.setAttribute("listprice", listTourprice);
//        request.setAttribute("listSchedule", listSchedule);
//        return mapping.findForward(ERR);
    }
}
